<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Activity;
use Illuminate\Support\Facades\Redirect;

use Illuminate\Support\Facades\Input;
use App\Http\Requests\Admin\Activity\StoreRequest;
use App\Http\Requests\Admin\Activity\UpdateRequest;
use Gate;
use App\Exceptions\Admin\PermissionDeniedException;
use App\Commands\OperationLogCommand;

class ActivityController extends Controller
{
    public function activity_gate()
    {
        if (!Gate::allows('goods','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->activity_gate();

        $search = array('name'=>'');
        $activitys = Activity::orderBy('sort_order','desc')->orderBy('id','desc');
        if (!empty($request->get('name'))){
            $activitys = $activitys->where('name', 'like', "%".trim($request->get('name'))."%");
            $search['name'] = $request->name;
        }//end if()
        $activitys = $activitys->paginate(10);

        return view('admin.activity.index',compact('activitys','search'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Activity $activity)
    {
        $this->activity_gate();

        return view('admin.activity.create',compact('activity'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreRequest $request,Activity $activity)
    {
        $this->activity_gate();

        $param = $request->all();
        $param['add_time'] = date('Y-m-d H:i:s');
        $activity = $activity->create($param);
        $this->dispatch(
            new OperationLogCommand('添加活动','Activity','id:'.$activity->id)
        );
        return Redirect('admin/activity');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $this->activity_gate();

        $activity = Activity::find($id);
        return view('admin.activity.edit',compact('activity'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRequest $request, $id)
    {
        $this->activity_gate();

        $activity = Activity::find($id);

        $activity->update($request->all());
        $this->dispatch(
            new OperationLogCommand('修改活动','Activity','id:'.$id)
        );

        return Redirect('admin/activity');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->activity_gate();

        $return = ['status'=>0, 'message'=>'', 'data'=>''];
        $activity = Activity::find($id);

        $activity->delete();
        $this->dispatch(
            new OperationLogCommand('删除活动','Activity','id:'.$id)
        );
        return $return;

        //return Redirect('admin/activity');
    }

    public function toggleShow(){

        $this->activity_gate();

        $id = Input::get("id");
        $is_recommend = Activity::where("id",$id)->pluck("is_recommend");
        $data = array();

        if($is_recommend==0){
            $data['is_recommend'] = 1;
            $data['id'] = $id;
            $res = Activity::where("id",$id)->update(['is_recommend'=>1]);
        }else{
            $data['is_recommend'] = 0;
            $data['id'] = $id;
            $res = Activity::where("id",$id)->update(['is_recommend'=>0]);
        }
        if($res != false){
            $data['info'] = true;
        }else{
            $data['info'] = false;
        }
        return $data;

    }

    /**
     * 排序
     * @route admin.activity.sort
     * @return Response
     */
    public function sort(Request $request)
    {

        $this->activity_gate();

        //$url = $request->all();
        $activity = $request->get('sort_order');
        //dd($url);

        if (is_array($activity)&&count($activity)>0)
        {
            foreach($activity as $k=>$v)
            {

                $sort_order = Activity::where('sort_order',$v)->get();

                Activity::find($k)->update(['sort_order' => $v]);

            }
        }

        return redirect('admin/activity');

    }
}
